# JSON格式转换器构建脚本

# 设置默认shell为bash
set shell := ["bash", "-c"]

# 定义变量
key_path := "$HOME/.chrome-extension-key.pem"

# 默认任务：显示帮助
default:
    @just --list

# 安装依赖
install:
    npm install
    npm install -g crx3

# 清理构建目录
clean:
    rm -rf build dist web-ext-artifacts dev-server
    rm -f *.crx *.zip private-key.pem

# 验证manifest文件
validate:
    @echo "验证 manifest.json..."
    @node -e "const manifest = require('./src/manifest.json'); console.log('Extension name:', manifest.name); console.log('Version:', manifest.version); if (!manifest.manifest_version || !manifest.name || !manifest.version) { throw new Error('Invalid manifest.json'); }"

# 准备构建目录
prepare: validate
    @echo "准备构建目录..."
    mkdir -p build
    cp -r src/* build/
    @echo "✅ 文件复制完成"

# 检查图标文件
check-icons:
    @echo "检查图标文件..."
    @if [ ! -d "src/icons" ]; then echo "❌ src/icons/ 目录不存在"; exit 1; fi
    @if [ ! -f "src/icons/icon16.png" ]; then echo "❌ 缺少图标: src/icons/icon16.png"; exit 1; fi
    @if [ ! -f "src/icons/icon32.png" ]; then echo "❌ 缺少图标: src/icons/icon32.png"; exit 1; fi
    @if [ ! -f "src/icons/icon48.png" ]; then echo "❌ 缺少图标: src/icons/icon48.png"; exit 1; fi
    @if [ ! -f "src/icons/icon128.png" ]; then echo "❌ 缺少图标: src/icons/icon128.png"; exit 1; fi
    @echo "✅ 所有图标文件存在"

# 生成私钥
generate-key:
    @echo "生成私钥..."
    @# 如果 key_path 和 private-key.pem 不存在，则生成新的私钥
    @if [ ! -f "{{key_path}}" ] && [ ! -f "private-key.pem" ]; then \
        openssl genrsa -out private-key.pem 2048; \
        echo "✅ 私钥已生成"; \
    fi

# 构建ZIP文件（旧版本，使用build-new代替）
build-zip: build-new
    @echo "构建 ZIP 文件..."
    @cd dist/extension && \
        VERSION=$(node -p "require('./manifest.json').version") && \
        zip -r "../../json-format-converter-v${VERSION}.zip" . && \
        echo "✅ ZIP 文件已生成: json-format-converter-v${VERSION}.zip"

# 构建CRX文件
build-crx: build-new generate-key
    @echo "构建 CRX 文件..."
    @VERSION=$(node -p "require('./src/manifest.json').version") && \
    KEY_PATH="private-key.pem" && \
    if [ -f "$KEY_PATH" ]; then \
        echo "✅ 使用私钥: $KEY_PATH"; \
    elif [ -f "{{key_path}}" ]; then \
        echo "✅ 使用全局私钥: {{key_path}}"; \
        KEY_PATH="{{key_path}}"; \
    else \
        echo "❌ 未找到私钥: $KEY_PATH"; \
        echo "请检查 private-key.pem 或 {{key_path}} 是否存在"; \
        exit 1; \
    fi && \
    crx3 -p $KEY_PATH -o "json-format-converter-v${VERSION}.crx" dist/extension/ && \
    echo "✅ CRX 文件已生成: json-format-converter-v${VERSION}.crx"

# 快速构建（仅ZIP）
quick: build-zip
    @echo "🚀 快速构建完成！"

# 完整构建（CRX + ZIP）
build: build-crx build-zip
    @echo "🎉 构建完成！"
    @ls -la *.crx *.zip 2>/dev/null || true

# 开发模式 - 自动重载
dev-watch: prepare
    @echo "🔄 开发模式 - 自动构建"
    @echo "================================"
    @echo "监听文件变化并自动重新构建到 build/ 目录"
    @echo "请在Chrome中加载 build/ 目录，然后刷新扩展"
    @echo ""
    @if command -v inotifywait >/dev/null 2>&1; then \
        while true; do \
            inotifywait -r -e modify,create,delete src/ && \
            echo "🔄 检测到文件变化，重新构建..." && \
            just prepare; \
        done \
    else \
        echo "❌ 需要安装 inotify-tools: sudo apt install inotify-tools"; \
    fi

# 检查扩展
lint:
    @echo "检查扩展..."
    @echo "验证 manifest.json 格式..."
    @node -e "const manifest = require('./src/manifest.json'); console.log('✅ Manifest 格式正确'); console.log('扩展名:', manifest.name); console.log('版本:', manifest.version);"
    @echo "检查必需文件..."
    @if [ -f "src/html/popup.html" ]; then echo "✅ popup.html 存在"; else echo "❌ popup.html 缺失"; fi
    @if [ -f "src/html/options.html" ]; then echo "✅ options.html 存在"; else echo "❌ options.html 缺失"; fi
    @if [ -f "src/js/background.js" ]; then echo "✅ background.js 存在"; else echo "❌ background.js 缺失"; fi
    @if [ -f "src/js/content.js" ]; then echo "✅ content.js 存在"; else echo "❌ content.js 缺失"; fi
    @if [ -f "src/js/popup.js" ]; then echo "✅ popup.js 存在"; else echo "❌ popup.js 缺失"; fi
    @if [ -f "src/js/options.js" ]; then echo "✅ options.js 存在"; else echo "❌ options.js 缺失"; fi
    @echo "✅ Chrome扩展检查完成"

# 显示版本信息
version:
    @node -p "'当前版本: ' + require('./src/manifest.json').version"

# 显示项目状态
status:
    @echo "📊 项目状态："
    @node -p "'版本: ' + require('./src/manifest.json').version"
    @echo "图标状态:"
    @if [ -f "src/icons/icon16.png" ]; then echo "  ✅ src/icons/icon16.png"; else echo "  ❌ src/icons/icon16.png"; fi
    @if [ -f "src/icons/icon48.png" ]; then echo "  ✅ src/icons/icon48.png"; else echo "  ❌ src/icons/icon48.png"; fi
    @if [ -f "src/icons/icon128.png" ]; then echo "  ✅ src/icons/icon128.png"; else echo "  ❌ src/icons/icon128.png"; fi
    @echo "构建文件:"
    @ls -la *.crx *.zip 2>/dev/null || echo "  无构建文件"

# 运行开发服务器
dev port="8080":
    @echo "🌐 启动开发服务器 (端口: {{port}})..."
    @node scripts/dev-server.js {{port}}

# 快速开发服务器
serve:
    @just dev 8080

# 使用新构建脚本构建
build-new:
    @echo "🚀 使用新构建脚本构建..."
    @node scripts/build.js

# 仅构建Firefox扩展
build-firefox:
    @echo "🦊 构建Firefox扩展..."
    @node scripts/build.js firefox

# 仅构建Chrome扩展
build-chrome:
    @echo "🌐 构建Chrome扩展..."
    @node scripts/build.js chrome

# 仅构建独立版本
build-standalone:
    @echo "🌐 构建独立版本..."
    @node scripts/build.js standalone

# 测试功能
test:
    @echo "🧪 启动测试服务器..."
    @npx http-server -p 8081 ./dist/extension

# 检查项目结构
check-structure:
    @echo "📋 检查项目结构..."
    @echo "源文件:"
    @find src -type f -name "*.html" -o -name "*.js" -o -name "*.css" -o -name "*.json" | sort
    @echo ""
    @echo "脚本文件:"
    @find scripts -type f -name "*.sh" | sort

# 验证项目完整性
check-all:
    @echo "🔍 验证项目完整性..."
    @node scripts/validate.js
    @echo ""
    @echo "🌍 验证i18n..."
    @node scripts/test-i18n.js

# 测试注释保留功能
test-comments:
    @echo "🔄 测试注释保留功能..."
    @node scripts/test-comments.js

# 测试i18n功能
test-i18n:
    @echo "🌍 测试i18n功能..."
    @node scripts/test-i18n.js

# 完整的发布流程
release: clean install validate build
    @echo "🎉 发布包已准备完成！"
    @echo "文件列表："
    @ls -la *.crx *.zip
